/*
 *                        .::::.
 *                      .::::::::.
 *                     :::::::::::
 *                  ..:::::::::::'
 *               '::::::::::::'
 *                 .::::::::::
 *            '::::::::::::::..
 *                 ..::::::::::::.
 *               ``::::::::::::::::
 *                ::::``:::::::::'        .:::.
 *               ::::'   ':::::'       .::::::::.
 *             .::::'      ::::     .:::::::'::::.
 *            .:::'       :::::  .:::::::::' ':::::.
 *           .::'        :::::.:::::::::'      ':::::.
 *          .::'         ::::::::::::::'         ``::::.
 *      ...:::           ::::::::::::'              ``::.
 *     ````':.          ':::::::::'                  ::::..
 *                        '.:::::'                    ':'````..
 *
 * @Author: your name
 * @Date: 2021-03-11 13:52:33
 * @LastEditTime: 2021-03-12 16:54:02
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \creator_i18n\assets\Script\i18n\i18nMgr.ts
 */

import * as i18nLabel from './i18nLabel';
import * as i18nSprite from './i18nSprite';

export class i18nMgr {
    // 当前语言
    private static language = '';
    private static labelArr: i18nLabel.i18nLabel[] = []; // i18nLabel 列表
    private static labelData: { [key: string]: string } = {}; // 文字配置
    private static spriteArr: i18nSprite.i18nSprite[] = []; // i18nSprite 列表

    /**
     * 检测初始化
     */
    private static _checkInit() {
        if (!this.language) {
            this.setLanguage('zh');
        }
    }

    /**
     * 设置语言
     */
    public static setLanguage(language: string) {
        if (this.language === language) {
            return;
        }
        this.language = language;
        this.reloadLabel();
        this.reloadSprite();
    }

    /**
     * 重置文本标签
     */
    private static reloadLabel() {
        let url = 'i18n/label/' + this.language;
        cc.resources.load(url, (err, data: cc.JsonAsset) => {
            if (err) {
                console.error(err);
                this.labelData = {};
            } else {
                this.labelData = data.json;
            }
            for (let one of this.labelArr) {
                one.resetValue();
            }
        });
    }

    /**
     * 重置精灵帧
     */
    private static reloadSprite() {
        for (let one of this.spriteArr) {
            one.resetValue();
        }
    }

    /**
     * 添加或移除 i18nLabel
     * @param label 多语言文本标签
     * @param isAdd 添加标志
     */
    public static _addOrDelLabel(label: i18nLabel.i18nLabel, isAdd: boolean) {
        if (isAdd) {
            this.labelArr.push(label);
        } else {
            let index = this.labelArr.indexOf(label);
            if (index !== -1) {
                this.labelArr.splice(index, 1);
            }
        }
    }

    /**
     * 获取 文本标签
     * @param opt 关键字
     * @param params 参数数组
     * @returns
     */
    public static _getLabel(opt: string, params: string[]): string {
        this._checkInit();
        if (params.length === 0) {
            return this.labelData[opt] || opt;
        }
        let str = this.labelData[opt] || opt;
        for (let i = 0; i < params.length; i++) {
            let reg = new RegExp('#' + i, 'g');
            str = str.replace(reg, params[i]);
        }
        return str;
    }

    /**
     * 添加或移除 i18nSprite
     * @param sprite 多语言精灵帧
     * @param isAdd 添加标志
     */
    public static _addOrDelSprite(sprite: i18nSprite.i18nSprite, isAdd: boolean) {
        if (isAdd) {
            this.spriteArr.push(sprite);
        } else {
            let index = this.spriteArr.indexOf(sprite);
            if (index !== -1) {
                this.spriteArr.splice(index, 1);
            }
        }
    }

    /**
     * 获取 精灵帧
     * @param path 精灵图片文件名
     * @param cb 完成回调
     */
    public static _getSprite(path: string, cb: (spriteFrame: cc.SpriteFrame) => void) {
        this._checkInit();
        cc.resources.load('i18n/sprite/' + this.language + '/' + path, cc.SpriteFrame, (err, spriteFrame: cc.SpriteFrame) => {
            if (err) {
                return cb(null);
            }
            cb(spriteFrame);
        });
    }
}
